require "Logic/Config"


UISysTips = {};
HelperFunc.SetUIEnv(UISysTips)
local this = UISysTips;

this.LuaBehaviour = nil;
this.ani = nil;
this.view = nil;

this.msgTable = {};
this.curMsg = nil;

this.showTips = false;
this.hasInit =false;
this.table_ErrorConfig ={};

this.sData = nil;

function UISysTips.Init(...)     
	sData = G.dataTable["MessageTips"];   
	local m = string.format("%#x",tostring(select(2,...)));
	local s = string.format("%#x",tostring(select(3,...)));
	m = string.sub(m,3,#m);
	s = string.sub(s,3,#s);
	if #s == 1 then
		s = "0"..s;
	end
	if #m == 1 then
		m = "0"..m;
	elseif #m == 0 then
		m = "00";
	end
	local str = m..s.."_"..select(1,...);
	local s = sData.Message[str];
	if s == nil then
		s = str;
	end
	curMsg  = s;
    this.LoadUI(function()
        msgTable[1] = curMsg
		--table.insert(msgTable,curMsg);
	end);
    
   
end

function UISysTips.LoadUI(cb)
    local name, package = getUIName(PanelNames.UISysTips);
    panelMgr:ShowUI(name, package, this.OnCreate,Config.useLocalUI);
    cb();
    showTips = true;
    this.Update()
    --[[if not hasInit then
        hasInit = true;
        --UpdateBeat:Add(this.Update, self);

    else
        if #msgTable == 0 then
            showTips = true;
        end
    end]]
end

function UISysTips.AddMsg(msg)
	if msg == nil then
		return;
	end
	
	curMsg = msg;
    this.LoadUI(function()
        msgTable[1] = curMsg
		--table.insert(msgTable,curMsg);
        this.cutMsgTable();
	end);
    
end

function UISysTips.AddMsgErrorCode(id)

    if id == 0 or id == nil  then
        return;
    end

    if table_ErrorConfig == nil or #table_ErrorConfig ==0 then
        table_ErrorConfig = G.dataTable["t_errorcode"]
    end
    local tempErrorCode=table_ErrorConfig[tostring(id)]
    if  tempErrorCode == nil then
        logError("errorCode  id:"..tostring(id))
        return;
    end
    curMsg =getLanguage(tempErrorCode.f_string_error);
    logError(curMsg)
    this.LoadUI(function()
        msgTable[1] = curMsg
        --table.insert(msgTable,curMsg);
        this.cutMsgTable();
    end);

end


function UISysTips.cutMsgTable()

    for i=#msgTable - 4, 1,-1 do
        table.remove(msgTable,i);
    end

end
function UISysTips.AddMsgById(id)
	local msg = getTextById(id);
	if msg == nil then
		msg = id;
	end
	curMsg = msg;
    this.LoadUI(function()
        msgTable[1] = curMsg
		--table.insert(msgTable,curMsg);
        this.cutMsgTable();
    end);
	 
end
--获得：%s*%s
function UISysTips.AddMsgByString(id,...)
    local msg = getTextById(id);
    if msg == nil then
        msg = id;
    end
    curMsg = string.format(msg,...);
    this.LoadUI(function()
        msgTable[1] = curMsg
        --table.insert(msgTable,curMsg);
        this.cutMsgTable();
    end);

end

function UISysTips.OnCreate(obj)
    view = obj;
    --gameObject = obj;
    --transform = obj.transform;
    --LuaBehaviour = transform:GetComponent('LuaBehaviour');
    --panelMgr:SetTopParent(obj);
    showTips = true;
    obj:GetChild("SysLb").text = ""
    this.bg = obj:GetChild("n1")
    this.bg.visible = false
    --transform:SetAsLastSibling(); 
end

function UISysTips.ShowInfo(str)
    if Util.IsNull(view) then return end
    this.bg.visible = true
    local lab_msg = view:GetChild("SysLb");
    lab_msg.text = stringFormatColor(str);
    ani = view:GetTransition("up");
    ani:Play(this.Complete)
    --Util.WaitForSeconds(function()
    --    gameObject:GetComponent("RectTransform").sizeDelta =  Vector2(lab_msg.gameObject:GetComponent("RectTransform").sizeDelta.x + 100,50);
    --end,0.01);
    view.visible = true;
    --gameObject:SetActive(true);
end

function UISysTips.OnDisable()
   -- UpdateBeat:Remove(this.Update, self);  
   showTips = false;
end

function UISysTips.OnDestroy()
    hasInit = false;
end

function  UISysTips.Update()
    if showTips then
        showTips = false;
        if #msgTable > 0 then
            this.PlayMove(msgTable[1]);
        end
    end
end

function UISysTips.PlayMove(str)
    this.ShowInfo(str);
end

function UISysTips.Complete()
    table.remove(msgTable,1);
    if not hasInit then
        return
    end
    if #msgTable > 0 then
        showTips = true;
    else
        view.visible = false;
    end 
end

return UISysTips;